package com.scitc.pleasurevideo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.datasql.VideoSourceSqlData;
import com.scitc.pleasurevideo.pojo.VideoSourceVo;

import java.util.List;

public class UserSettingVideoSourceListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<VideoSourceVo> items;
    private Context context;


    public UserSettingVideoSourceListAdapter(Context context, List<VideoSourceVo> items){
        this.items=items;
        this.context=context;
    }

    public void setItemIsSelect(int position){
        if(position<0||position>=items.size()){
            return;
        }
        this.items.get(position).setSource_url_is_select(this.items.get(position).getSource_url_is_select());
        VideoSourceSqlData.getDbDataManager().updataVideoSource(this.items.get(position));
        notifyDataSetChanged();
    }

    public Integer getSelectItemCount(){
        if(items==null||items.size()<1){
            return 0;
        }
        Integer count=0;
        for(VideoSourceVo vo:items){
            if(vo.getSource_url_is_select()){
                count++;
            }
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if(position<0||position>=items.size()){
            return -1;
        }
        VideoSourceVo news=items.get(position);
        if(news.getSource_url_is_select()){
            return 0;
        }else {
            return 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView title;
        private CheckBox cbCheckbox;
        private TextView tvDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title=itemView.findViewById(R.id.title);
            cbCheckbox=itemView.findViewById(R.id.cbCheckbox);
            tvDelete=itemView.findViewById(R.id.tvDelete);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater=LayoutInflater.from(context);
        View view=layoutInflater.inflate(R.layout.item_setting_video_source_list,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VideoSourceVo news=items.get(position);
        if(holder instanceof ViewHolder){
            ViewHolder viewHolder=(ViewHolder)holder;
            viewHolder.title.setText(news.getSource_url_title());
            viewHolder.cbCheckbox.setChecked(news.getSource_url_is_select());
            viewHolder.tvDelete.setVisibility(View.VISIBLE);
            viewHolder.itemView.setOnClickListener(view -> setItemIsSelect(position));
        }
    }

    @Override
    public int getItemCount() {
        if(items==null||items.size()<1){
            return 0;
        }
        return items.size();
    }
}
